package com.example.restatr.Atcivity.Main;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.restatr.Atcivity.ActivityManager.HuoDongAt;
import com.example.restatr.Atcivity.Bus.BusAt;
import com.example.restatr.Atcivity.Charity.CharityAt;
import com.example.restatr.Atcivity.CitySubway.CitySubwayAt;
import com.example.restatr.Atcivity.DataAnalysis.DataAnalysisAt;
import com.example.restatr.Atcivity.DigitalLibrary.DigitalLibraryAt;
import com.example.restatr.Atcivity.GarbageClassification.GarbageClassificationAt;
import com.example.restatr.Atcivity.Hospital.HospitalAt;
import com.example.restatr.Atcivity.JobSearch.JobSearchAt;
import com.example.restatr.Atcivity.KitchenHelper.KitchenHelperAt;
import com.example.restatr.Atcivity.LawyerConsultation.LawyerConsultationAt;
import com.example.restatr.Atcivity.LivePayment.LivePaymentAt;
import com.example.restatr.Atcivity.LogisticsQuery.LogisticsQueryAt;
import com.example.restatr.Atcivity.Movie.MovieAt;
import com.example.restatr.Atcivity.Park.ParkAt;
import com.example.restatr.Atcivity.PetHospital.PetHospitalAt;
import com.example.restatr.Atcivity.SeekHouse.SeekHouseAt;
import com.example.restatr.Atcivity.TakeOut.TakeOutAt;
import com.example.restatr.Atcivity.Traffic.TrafficAt;
import com.example.restatr.Atcivity.VolunteerService.VolunteerServiceAt;
import com.example.restatr.Atcivity.YouthInn.YouthInnAt;
import com.example.restatr.Bean.ServiceBean;
import com.example.restatr.Atcivity.GovServiceHotline.GovServiceHotlineAt;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class MoreAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private List<Integer> typeIcons = new ArrayList<>();
    private List<String> typeTitles = new ArrayList<>();
    private HashMap<Integer, List<ServiceBean.RowsBean>> listMap = new HashMap<>();
    private RecyclerView rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_at);


        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);

        rv = findViewById(R.id.rv);
        Tools.SetBarTitle(tb, barTitle, "更多服务", search, this, false);
        Collections.addAll(typeIcons, R.drawable.ic_baseline_emoji_transportation_24, R.drawable.ic_baseline_storefront_24, R.drawable.ic_baseline_directions_car_24);
        Collections.addAll(typeTitles, "生活服务", "便民服务", "车主服务");
        GetMore();
    }

    private void GetMore() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String get = Tools.Get("/prod-api/api/service/list", "");
                List<ServiceBean.RowsBean> rowsBeans = new ArrayList<>();
                rowsBeans.addAll(new Gson().fromJson(get, ServiceBean.class).getRows());
                List<ServiceBean.RowsBean> 生活服务 = rowsBeans.stream().filter(rowsBean -> rowsBean.getServiceType().equals("生活服务")).collect(Collectors.toList());
                List<ServiceBean.RowsBean> 便民服务 = rowsBeans.stream().filter(rowsBean -> rowsBean.getServiceType().equals("便民服务")).collect(Collectors.toList());
                List<ServiceBean.RowsBean> 车主服务 = rowsBeans.stream().filter(rowsBean -> rowsBean.getServiceType().equals("车主服务")).collect(Collectors.toList());
                listMap.put(0, 生活服务);
                listMap.put(1, 便民服务);
                listMap.put(2, 车主服务);
                runOnUiThread(() -> {
                    rv.setAdapter(new rvAdapter());
                });
            }
        }).start();
    }


    class rvAdapter extends RecyclerView.Adapter<rvAdapter.rvViewHolder> {
        @NonNull
        @Override
        public rvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.more_item_layout, parent, false);
            return new rvViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull rvViewHolder holder, int position) {
            holder.icon.setImageResource(typeIcons.get(position));
            holder.title.setText(typeTitles.get(position));
            holder.gv.setAdapter(new cardAdapt(listMap.get(position)));
            GridLayoutManager gridLayoutManager = new GridLayoutManager(MoreAt.this, 4);
            holder.gv.setLayoutManager(gridLayoutManager);
        }

        @Override
        public int getItemCount() {
            return typeIcons.size();
        }

        class rvViewHolder extends RecyclerView.ViewHolder {
            private ImageView icon;
            private TextView title;
            private RecyclerView gv;

            public rvViewHolder(@NonNull View itemView) {
                super(itemView);
                icon = itemView.findViewById(R.id.icon);
                title = itemView.findViewById(R.id.title);
                gv = itemView.findViewById(R.id.gv);
            }
        }
    }

    class cardAdapt extends RecyclerView.Adapter<cardAdapt.cardViewHolder> {
        private List<ServiceBean.RowsBean> rowsBeanList = new ArrayList<>();

        public cardAdapt(List<ServiceBean.RowsBean> rowsBeanList) {
            this.rowsBeanList = rowsBeanList;
        }

        @NonNull
        @Override
        public cardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.card_item_layout, null, false);
            return new cardViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull cardViewHolder holder, int position) {
            Glide.with(MoreAt.this)
                    .load(Tools.ip + rowsBeanList.get(position).getImgUrl())
                    .placeholder(R.drawable.bj)
                    .error(R.drawable.bj)
                    .into(holder.icon);
            holder.title.setText(rowsBeanList.get(position).getServiceName());
            holder.itemView.setTag(rowsBeanList.get(position).getServiceName());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getTag().toString()) {
                        case "城市地铁":
                            Tools.StartActivity(MoreAt.this, CitySubwayAt.class);
                            break;
                        case "生活缴费":
                            Tools.StartActivity(MoreAt.this, LivePaymentAt.class);
                            break;
                        case "看电影":
                            Tools.StartActivity(MoreAt.this, MovieAt.class);
                            break;
                        case "活动管理":
                            Tools.StartActivity(MoreAt.this, HuoDongAt.class);
                            break;
                        case "外卖订餐":
                            Tools.StartActivity(MoreAt.this, TakeOutAt.class);
                            break;
                        case "找房子":
                            Tools.StartActivity(MoreAt.this, SeekHouseAt.class);
                            break;
                        case "找工作":
                            Tools.StartActivity(MoreAt.this, JobSearchAt.class);
                            break;
                        case "停哪儿":
                            Tools.StartActivity(MoreAt.this, ParkAt.class);
                            break;
                        case "智慧交管":
                            Tools.StartActivity(MoreAt.this, TrafficAt.class);
                            break;
                        case "门诊预约":
                            Tools.StartActivity(MoreAt.this, HospitalAt.class);
                            break;
                        case "智慧巴士":
                            Tools.StartActivity(MoreAt.this, BusAt.class);
                            break;
                        case "政府服务热线":
                            Tools.StartActivity(MoreAt.this, GovServiceHotlineAt.class);
                            break;
                        case "法律咨询":
                            Tools.StartActivity(MoreAt.this, LawyerConsultationAt.class);
                            break;
                        case "志愿服务":
                            Tools.StartActivity(MoreAt.this, VolunteerServiceAt.class);
                            break;
                        case "数字图书馆":
                            Tools.StartActivity(MoreAt.this, DigitalLibraryAt.class);
                            break;
                        case "垃圾分类":
                            Tools.StartActivity(MoreAt.this, GarbageClassificationAt.class);
                            break;
                        case "青年驿站":
                            Tools.StartActivity(MoreAt.this, YouthInnAt.class);
                            break;
                        case "爱心捐赠":
                            Tools.StartActivity(MoreAt.this, CharityAt.class);
                            break;
                        case "物流查询":
                            Tools.StartActivity(MoreAt.this, LogisticsQueryAt.class);
                            break;
                        case "宠物医院":
                            Tools.StartActivity(MoreAt.this, PetHospitalAt.class);
                            break;
                        case "厨房助手":
                            Tools.StartActivity(MoreAt.this, KitchenHelperAt.class);
                            break;
                        case "数据分析":
                            Tools.StartActivity(MoreAt.this, DataAnalysisAt.class);
                            break;
                        case "教育机构":
                        case "医疗健康":
                            startActivity(new Intent(MoreAt.this, NullAt.class));
                            break;
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeanList.size();
        }

        class cardViewHolder extends RecyclerView.ViewHolder {
            private ImageView icon;
            private TextView title;

            public cardViewHolder(@NonNull View itemView) {
                super(itemView);
                icon = itemView.findViewById(R.id.icon);
                title = itemView.findViewById(R.id.text);
            }
        }
    }

}